/******************************************************************************\
    File.hpp contains a class of windows file object.
    Copyright (C) 2008  Victor Vinogradov (fly3333@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
\******************************************************************************/

#ifndef __FILE_HPP
#define __FILE_HPP

#include ".\Object.hpp"
#include ".\Handle.hpp"

class cFile : public cHandle
{
public:
	bool Create( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile )
	{
		HANDLE file_handle = CreateFile( lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile );
		if( file_handle == INVALID_HANDLE_VALUE )
			return false;
		cSubHandle *sub_handle;
		try
		{
			sub_handle = new cSubHandle( file_handle );
		}
		catch( ... )
		{
			return false;
		}
		SwitchToSubObject( sub_handle );
		return true;
	}
	//void Close()
	//{
	//	SwitchToSubObject( NULL );
	//}
	//operator HANDLE() const 
	//{
	//	if( GetSubObject() == NULL )
	//		return INVALID_HANDLE_VALUE;
	//	return GetSubObject()->Handle();
	//}
	long long Size() const
	{
		if( GetSubObject() == NULL )
			return -1;
		LARGE_INTEGER file_size;
		if( GetFileSizeEx( GetSubObject()->Handle(), &file_size ) == FALSE )
			return -1;
		return file_size.QuadPart;
	}
	bool Read( LPVOID buffer, DWORD number_of_bytes_to_read, LPDWORD number_of_bytes_read, LPOVERLAPPED overlapped )
	{
		if( GetSubObject() == NULL )
			return false;
		return ReadFile( GetSubObject()->Handle(), buffer, number_of_bytes_to_read, number_of_bytes_read, overlapped ) != FALSE;
	}
	bool Write( LPVOID buffer, DWORD number_of_bytes_to_write, LPDWORD number_of_bytes_written, LPOVERLAPPED overlapped )
	{
		if( GetSubObject() == NULL )
			return false;
		return WriteFile( GetSubObject()->Handle(), buffer, number_of_bytes_to_write, number_of_bytes_written, overlapped ) != FALSE;
	}
	bool Flush()
	{
		if( GetSubObject() == NULL )
			return false;
		return FlushFileBuffers( GetSubObject()->Handle() ) != FALSE;
	}
	bool SetEOF()
	{
		if( GetSubObject() == NULL )
			return false;
		return SetEndOfFile( GetSubObject()->Handle() ) != FALSE;
	}
	DWORD SetPointer( LONG distance_to_move, DWORD move_method )
	{
		if( GetSubObject() == NULL )
			return INVALID_SET_FILE_POINTER;
		return SetFilePointer( GetSubObject()->Handle(), distance_to_move, NULL, move_method );
	}
	bool GetTimes( LPFILETIME creation_time, LPFILETIME last_access_time, LPFILETIME last_write_time ) const
	{
		if( GetSubObject() == NULL )
			return false;
		return GetFileTime( GetSubObject()->Handle(), creation_time, last_access_time, last_write_time ) != FALSE;
	}
};

#endif